package com.uwantsoft.business.facade;

import com.uwantsoft.business.facade.dto.address.LatLngDto;
import com.uwantsoft.business.facade.dto.parking.*;
import com.uwantsoft.business.service.CityServiceImpl;
import com.uwantsoft.business.service.ParkServiceImpl;
import com.uwantsoft.business.service.domain.city.City;
import com.uwantsoft.business.service.domain.park.Park;
import com.uwantsoft.util.paging.DefaultPaginatedList;
import com.uwantsoft.util.paging.PagingListLoader;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author LeiPeichen.
 */
@Component(value = "parkingFacade")
public class ParkingFacadeImpl {
    @Resource(name = "parkService")
    private ParkServiceImpl parkService;

    @Resource(name = "cityService")
    private CityServiceImpl cityService;

    public List<ParkingDto> obtainParks(String region, String latitude, String longitude){
        List<ParkingDto> parkingDtoList = new ArrayList<ParkingDto>();
        if(longitude.equals("104.07234") && latitude.equals("30.663534")){
            return parkingDtoList;
        }
        ParkingDto parkingDtoOne = new ParkingDto();
        parkingDtoOne.setGuid("3999bd95-41ed-493e-bd51-50f3017054e6");
        parkingDtoOne.setName("IFS国际金融中心停车场");
        parkingDtoOne.setSpecialty("CHEAPEST");
        parkingDtoOne.setAddress("成都市锦江区北纱帽街IFS国际金融中心");
        parkingDtoOne.setStatus("AVAILABLE");
        parkingDtoOne.setStatusDescription("本次停车免收预订费,车位将为您保留一小时,为了避免资源闲置,我们将收取5元押金,请您尽快到达该停车场,结束后,我们将返回到您的个人账户中。");
        PriceDto priceDto = new PriceDto();
        priceDto.setHours("3");
        priceDto.setCost("48");
        parkingDtoOne.setPrice(priceDto);
        String [] tags = new String[]{"妇女专用","车位宽大"};
        parkingDtoOne.setType("商业停车场");
        parkingDtoOne.setTags(tags);
        parkingDtoOne.setQueueLength("23");
        parkingDtoOne.setWalktime("23");
        parkingDtoOne.setDistance("500");
        LatLngDto latLngDtoOne = new LatLngDto();
        latLngDtoOne.setLongitude("104.086923");
        latLngDtoOne.setLatitude("30.660459");
        parkingDtoOne.setLatLng(latLngDtoOne);
        parkingDtoOne.setFeeRule("起价5元，含一小时，每小时2元");
        parkingDtoList.add(parkingDtoOne);



        ParkingDto parkingDtoTwo = new ParkingDto();
        parkingDtoTwo.setGuid("9861bd05-49ed-560e-bd51-50f3017054e6");
        parkingDtoTwo.setName("英郡停车场");
        parkingDtoTwo.setSpecialty("CHEAPEST");
        parkingDtoTwo.setAddress("天华路299号");
        parkingDtoTwo.setStatus("WAITING");
        parkingDtoTwo.setStatusDescription("现在停车人数比较多,如有空闲车位,我们将以消息推送给你,请注意查收,车位空出时,介时将为您保留1分钟,超时需要重新排队!");
        PriceDto priceDtoTwo = new PriceDto();
        priceDtoTwo.setHours("3");
        priceDtoTwo.setCost("48");
        parkingDtoTwo.setPrice(priceDto);
        String [] tagsTwo = new String[]{"女司机专用","车位宽广"};
        parkingDtoTwo.setType("商业停车场");
        parkingDtoTwo.setTags(tagsTwo);
        parkingDtoTwo.setQueueLength("1");
        parkingDtoTwo.setWalktime("12");
        parkingDtoTwo.setDistance("300");
        LatLngDto latLngDtoTwo = new LatLngDto();
        latLngDtoTwo.setLongitude("104.078592");
        latLngDtoTwo.setLatitude("30.554244");
        parkingDtoTwo.setLatLng(latLngDtoTwo);
        parkingDtoTwo.setFeeRule("起价5元，含一小时，每小时2.5元");
        parkingDtoList.add(parkingDtoTwo);
        return parkingDtoList;
    }
    public DefaultPaginatedList obtainParkByPage(final ParkOverviewQueryCondition condition){
        return new DefaultPaginatedList(condition).loadData(new PagingListLoader() {
            @Override
            public List loadList() {
                List<ParkOverViewDto> parkOverViewDtoList = new ArrayList<ParkOverViewDto>();
                List<Park> parks = parkService.findParks(condition);
                for (Park park : parks){
                    parkOverViewDtoList.add(park.convertParkOverView());
                }
                return parkOverViewDtoList;
            }
            @Override
            public int loadFullListSize() {
                return parkService.findParkTotalAmount(condition);
            }
        });
    }


    public void addOrUpdate(ParkFormDto parkFormDto){
        Park park = parkService.findPark(parkFormDto.getGuid());
        City city = cityService.findCity(parkFormDto.getCityGuid());
        if(park == null){
            Park createPark = Park.createPark(parkFormDto,city);
            parkService.createPark(createPark);
        }else{
            park.update(parkFormDto,city);
        }
    }
    public ParkFormDto obtainPark(String guid){
        Park park = parkService.findPark(guid);
        if(park != null){
            return park.convert();
        }
        return new ParkFormDto();
    }

    public ParkDetailDto obtainParkDetail(String guid){
        Park park = parkService.findParkDetail(guid);
        if(park != null){
            return park.convertParkDetail();
        }
        return new ParkDetailDto();
    }

    public void lockPark(String guid,boolean lock) {
        parkService.lockPark(guid, lock);
    }
}
